class Solution:
    def findMin(self, nums: List[int]) -> int:
        left, right = -1, len(nums) - 1
        while left + 1 < right:
            mid = (left + right)//2
            if nums[mid] < nums[-1]:
                right = mid
            else:
                left = mid
        return right
    
    def lower_bound(self, nums:List[int], left: int, right: int, target: int) -> int:
        while left + 1 < right:
            mid = (left + right) // 2
            if nums[mid] >= target:
                right = mid
            else:
                left = mid
        return right if nums[right] == target else - 1
    
    def search(self, nums: List[int], target: int) -> int:
        i = self.findMin(nums)
        if target > nums[-1]:
            return self.lower_bound(nums, -1, i, target)
        return self.lower_bound(nums, i - 1, len(nums), target)